<?php

namespace App\Containers\AppSection\Moderation\Actions;

use App\Containers\AppSection\Moderation\Tasks\DeleteAllRemarksByModerationIdTask;
use App\Containers\AppSection\Log\Tasks\CreateLogTask;
use App\Containers\AppSection\Moderation\Tasks\CreateRemarkTask;
use App\Containers\AppSection\Moderation\Tasks\FindModerationByIdTask;
use App\Containers\AppSection\Moderation\Tasks\UpdateModerationTask;
use App\Containers\AppSection\Pledge\Tasks\UpdatePledgeTask;
use App\Containers\AppSection\Project\Tasks\UpdateAnalyticTask;
use App\Containers\AppSection\Project\Tasks\UpdateConditionTask;
use App\Containers\AppSection\Project\Tasks\UpdateGovernmentTask;
use App\Containers\AppSection\Project\Tasks\UpdateProjectTask;
use App\Ship\Parents\Actions\Action;
use App\Ship\Parents\Requests\Request;

class ModeratorCheckModerationAction extends Action
{

    public function run($id, Request $request)
    {
        $moderation = app(FindModerationByIdTask::class)->run($id);

        app(DeleteAllRemarksByModerationIdTask::class)->run($moderation->id);

        if (!empty($request->getInputByKey('remarks'))) {
            foreach ($request->getInputByKey('remarks') as $remark) {
                app(CreateRemarkTask::class)->run([
                    'message' => $remark['message'],
                    'type_key' => $remark['type_key'] ?? null,
                    'moderation_id' => $moderation->id,
                    'profile_id' => $request->profile()->id
                ]);
            }
        }

        app(UpdateConditionTask::class)->run($moderation->project->condition->id, [
            'percent' => $request->getInputByKey('condition.percent'),
            'loan' => $request->getInputByKey('condition.loan'),
            'deffer_period' => $request->getInputByKey('condition.deffer_period') ?? null,
            'penalty_per_day' => $request->getInputByKey('condition.penalty_per_day') ?? null,
            'charge_penalty_first_day' => $request->getInputByKey('condition.charge_penalty_first_day') ?? true,
            'percents_calculation' => $request->getInputByKey('condition.percents_calculation') ?? null,
            'output_key' => $request->getInputByKey('condition.output_key'),
        ]);

        app(UpdateAnalyticTask::class)->run($moderation->project->analytic->id, [
            'credit_history' => $request->getInputByKey('analytic.credit_history'),
            'suspension' => $request->getInputByKey('analytic.suspension'),
            'balance' => $request->getInputByKey('analytic.balance'),
            'revenue' => $request->getInputByKey('analytic.revenue'),
            'net_profit' => $request->getInputByKey('analytic.net_profit'),
            'nds' => $request->getInputByKey('analytic.nds'),
            'contributions' => $request->getInputByKey('analytic.contributions'),
            'total_paid' => $request->getInputByKey('analytic.total_paid'),
            'debt' => $request->getInputByKey('analytic.debt'),
            'entries_egrul' => $request->getInputByKey('analytic.entries_egrul'),
            'arbitration_cases' => $request->getInputByKey('analytic.arbitration_cases'),
            'execution_proceedings' => $request->getInputByKey('analytic.execution_proceedings'),
            'availability_lists' => $request->getInputByKey('analytic.availability_lists'),
            'government_purchases' => $request->getInputByKey('analytic.government_purchases'),
        ]);

        if ($moderation->project->profile->hasLegalForm('entity')) {
            app(UpdateGovernmentTask::class)->run($moderation->project->government->id, [
                'type_key' => $request->getInputByKey('government.type_key'),
                'participants' => $request->getInputByKey('government.participants'),
                'body_name' => $request->getInputByKey('government.body_name'),
                'participants_name' => $request->getInputByKey('government.participants_name'),
                'persons_information' => $request->getInputByKey('government.persons_information'),
            ]);
        }

        foreach ($request->getInputByKey('pledges') as $key => $pledge) {
            app(UpdatePledgeTask::class)->run($request->getInputByKey("pledges.$key.id"), [
                'collateral_value' => $request->getInputByKey("pledges.$key.collateral_value"),
                'description' => $request->getInputByKey("pledges.$key.description"),
            ]);
        }

        $moderated_at = null;
        if ($request->getInputByKey('type') === 'projects_collecting') {
            $moderated_at = now();
        }

        app(UpdateProjectTask::class)->run($moderation->project->id, [
            'moderation_status_key' => $request->getInputByKey('type'),
            'rate' => [
                'support' => $request->getInputByKey('rate.support'),
                'tariff' => $request->getInputByKey('rate.tariff'),
            ],
            'moderated_at' => $moderated_at
        ]);

        app(UpdateModerationTask::class)->run($moderation->id, [
            'moderator_id'=> $request->profile()->id
        ]);


        $log = '';
        switch ($request->getInputByKey('type')) {
            case 'projects_collecting':
                $log = "Проект №{$moderation->project->id} принят и отправлен на сбор инвестиций";
                break;
            case 'projects_returned':
                $log = "Проект №{$moderation->project->id} отправлен на доработку";
                break;
            case 'projects_rejected':
                $log = "Проект №{$moderation->project->id} отклонен";
                break;
        }


        app(CreateLogTask::class)->run($moderation->project->profile->id, $log . ' (модератор)', 'moderation', $moderation->id);
        app(CreateLogTask::class)->run($request->profile()->id, $log, 'moderation', $moderation->id);
    }
}
